<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentType">
              <j-search-select-tag
                dict="hf_purchase_request_type"
                v-model="model.documentType"
                placeholder="请输入单据类型"
              ></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentTime">
              <j-date placeholder="请选择单据日期" v-model="model.documentTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
              <j-dict-select-tag
                dictCode="hf_supplier,name,id"
                v-model="model.supplierId"
                placeholder="请输入供应商"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="供应商订单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="supplierOrderSerial"
            >
              <a-input v-model="model.supplierOrderSerial" placeholder="请输入供应商订单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预计到货日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrivalTime">
              <j-date placeholder="请选择预计到货日期" v-model="model.arrivalTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="实际到货日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="actualArrivalTime"
            >
              <j-date placeholder="请选择实际到货日期" v-model="model.actualArrivalTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="请购人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-dict-select-tag
                dictCode="sys_user,realname,username"
                v-model="model.operator"
                placeholder="请输入请购人"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.status === '已审核'">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <j-dict-select-tag
                dictCode="sys_user,realname,username"
                placeholder="请输入审核员"
                disabled
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.status === '已审核'">
            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <j-date
                placeholder="请选择审核时间"
                v-model="model.checkTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import store from '@/store'
import dayjs from 'dayjs'

export default {
  name: 'HfPurchaseRequestModal',
  components: {},
  props: {
    //是否审核
    isChecked: {
      type: Number
    }
  },
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentTime: dayjs().format('YYYY-MM-DD'),
        documentType: 1
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        documentType: [{ required: true, message: '请输入单据类型!' }],
        supplierId: [{ required: true, message: '请输入供应商id!' }],
        documentTime: [{ required: true, message: '请输入单据日期!' }]
      },
      url: {
        add: '/purchase/hfPurchaseRequest/add',
        edit: '/purchase/hfPurchaseRequest/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  watch: {},
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      if (this.isChecked === 0) {
        this.close()
        return
      }
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
